﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartParking.Client.DAL
{
    public  class WebDataAccess
    {
        protected string domain = "http://localhost:5022/api/";
        public Task<string> GetDatas(string url) {
            using (HttpClient client = new HttpClient()) {
                var resp = client.GetAsync($"{domain}{url}").GetAwaiter().GetResult();
                return resp.Content.ReadAsStringAsync();
            }
        }
        private MultipartFormDataContent GetFormData(Dictionary<string, HttpContent> contents) 
        {
            var postContext = new MultipartFormDataContent();
            string boundary = $"-----------{DateTime.Now.Ticks.ToString("x")}---------------";
            postContext.Headers.Add("ContentType", $"muiltipart/form-data,boundary={boundary}");
            foreach (var item in contents)
            {
                postContext.Add(item.Value, item.Key);
            }
            return postContext;
        }
        public Task<string> PostDatas(string url,Dictionary<string, HttpContent> contents)
        {
            using (HttpClient client = new HttpClient())
            {
                var resp = client.PostAsync($"{domain}{url}", GetFormData(contents)).GetAwaiter().GetResult();
                return resp.Content.ReadAsStringAsync();
            }
        }
        public Task<string> PostDatas(string url, HttpContent contents)
        {
            using (HttpClient client = new HttpClient())
            {
                var resp = client.PostAsync($"{domain}{url}", contents).GetAwaiter().GetResult();
                return resp.Content.ReadAsStringAsync();
            }
        }
    }
}
